package com.thinkgem.jeesite.modules.fuct.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.thinkgem.jeesite.common.mapper.JaxbMapper;
import com.thinkgem.jeesite.common.utils.basetype.strings.StringUtil;
import com.thinkgem.jeesite.common.web.webservice.Request;

public class WebserviceUtil {

	public static String sendHttpPost(Request<?> request,String address,Class<?> clazz) throws Exception {
		String responseXml = null;
		URL url = new URL(address);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setUseCaches(false);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("connection", "Keep-Alive");
		conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
		conn.setRequestProperty("Charsert", "UTF-8");
		conn.setRequestProperty("Content-Type", "text/html");
		OutputStream out = conn.getOutputStream();
		String requireXml = JaxbMapper.toXml(request, clazz);
		out.write(StringUtil.toByteArray(requireXml));
		out.flush();
		out.close();
		BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = reader.readLine()) != null) {
			sb.append(line);
		}
		responseXml = sb.toString();
		return responseXml;
	}
	
	public static String sendHttpPost(String requireXml,String address) throws Exception {
		String responseXml = null;
		URL url = new URL(address);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setUseCaches(false);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("connection", "Keep-Alive");
		conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
		conn.setRequestProperty("Charsert", "UTF-8");
		conn.setRequestProperty("Content-Type", "text/html");
		OutputStream out = conn.getOutputStream();
		out.write(StringUtil.toUTF8ByteArray(requireXml));
		out.flush();
		out.close();
		BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = reader.readLine()) != null) {
			sb.append(line);
		}
		responseXml = sb.toString();
		return responseXml;
	}
	
	/*public static <T> List<T> getResponseEntity(String responseXml,Class<T> clazz) throws Exception{
		JavaType paramType = ClassUtil.getParamJavaType(Response.class,clazz);
		Response<T> root = mapper.readValue(responseXml,paramType);
		ResponseHead head = root.getHead();
		List<T> data = null;
		if(head.getResult()!=null){
			if(!"00".equals(head.getResult().getCode())){
				List<String> errorList = head.getResult().getErrorList();
				String error = "";
				if(errorList!=null && errorList.size()>0){
					for(int i=0;i<errorList.size();i++){
						error+=errorList.get(i)+",";
					}
				}else{
					error = head.getResult().getMsg();
				}
				throw new ParamException("操作失败,原因为："+error);
			}else{
				if(root.getBody()!=null){
					data = root.getBody().getList();
				}
			}
		}
		return data;
	}*/
	
}
